//
//  SeachView.swift
//  WeiBoItem
//
//  Created by jss on 16/4/16.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit

class SeachView: UIView {

    @IBOutlet weak var textFileRightConstrain: NSLayoutConstraint!
    @IBOutlet weak var cancalBtn: UIButton!
    @IBOutlet weak var textFileView: UITextField!
    //通过类方法加载XIB视图
       class func seachView() ->SeachView {
        
        return NSBundle.mainBundle().loadNibNamed("SearchView", owner:nil, options: nil) .last as! SeachView

    }
    
   
    
    //控件从xib里唤醒(只要加载xib)  就会调用此方法(里面的子控件全部连线完毕)
    override func awakeFromNib() {
        super.awakeFromNib()
        
//        print("awakeFromNib 方法被调用了")
        
//        print(searchImageView)
        
        //避免图形压扁设置成正方形
        searchImageView.frame.size = CGSizeMake(self.frame.size.height, self.frame.size.height)
//        searchImageView.frame.size = CGSize(width: self.frame.size.height, height: self.frame.size.height)
        self.textFileView.leftView = searchImageView
        
        self.textFileView.leftViewMode = .Always
        
    }
    
    
    
    
    //textFile的将要编辑的时候调用
    @IBAction func textFileBeginEditing(sender: AnyObject) {
        //一点击修改约束
        textFileRightConstrain.constant = self.cancalBtn.frame.size.width;
        
        //实现动画
        UIView.animateWithDuration(0.5) { () -> Void in
            
            //重新布局
            self.layoutIfNeeded()
        }
        
//        print("动画效果实现了")
    }
    
    //点击取消按钮
    @IBAction func didClickCancalBtn(sender: AnyObject) {
        
        //点击取消按钮修改约束
        textFileRightConstrain.constant = 0
        
        //执行动画
        UIView.animateWithDuration(0.5) { () -> Void in
            //重新布局
            self.layoutIfNeeded()
        }
        
        //并且取消第一响应者
        self.textFileView.resignFirstResponder()
    }
    
    
   //懒加载做左边的imageView(搜索图片)
    
    lazy var searchImageView: UIImageView = {()->UIImageView in
        
        
        //同时可以添加highlightedImage
//        let imageView = UIImageView(image: UIImage(named: "tabbar_discover"), highlightedImage: UIImage(named: "tabbar_discover_selected"))
       
        let imageView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
      //设置图片内容的模式
      imageView.contentMode = .Center
     
        return imageView
        
    }()
  
}










